<template>
  <div class="goods-relevant">
    <div class="header">
      <i class="icon"></i>
      <span class="title">{{ goodsId ? "同类商品" : "猜你喜欢" }}</span>
    </div>
    <!-- 此处使用改造后的xtx-carousel.vue -->
    <XtxCarousel :bannerList="relevantList"></XtxCarousel>
  </div>
</template>

<script>
import { getRelevantGoods } from "@/api/goods";
import { ref } from "vue";
import _ from "lodash";
export default {
  name: "GoodsRelevant",
  props: {
    goodsId: {
      type: String,
    },
  },
  setup(props) {
    const { relevantList, getData } = useRelativeGoods(props.goodsId);
    getData();
    return {
      relevantList,
    };
  },
};
// 获取相关商品数据;
function useRelativeGoods(id) {
  // 定义每页显示数据条数
  const size = 4;
  // 用于储存修改个格式后的数据
  const relevantList = ref([]);
  const getData = () => {
    getRelevantGoods(id).then((data) => {
      // 计算有几页数据
      // const total = Math.ceil(data.result.length / size);

      // 修改数据格式
      // for (let i = 0; i < total; i++) {
      //   relevantList.value.push(data.result.slice(i * size, i * size + size));
      // }
      relevantList.value = _.chunk(data.result, size);
    });
  };
  return {
    getData,
    relevantList,
  };
}
</script>

<style scoped lang="less">
.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
  .header {
    height: 80px;
    line-height: 80px;
    padding: 0 20px;
    .title {
      font-size: 20px;
      padding-left: 10px;
    }
    .icon {
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid @xtxColor;
      border-right: 4px solid @xtxColor;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &::before {
        content: "";
        width: 10px;
        height: 10px;
        position: absolute;
        left: 0;
        top: 2px;
        background: lighten(@xtxColor, 40%);
      }
    }
  }
}
.goods-relevant {
  :deep(.xtx-carousel) {
    height: 380px;
    .carousel {
      &-indicator {
        bottom: 30px;
        span {
          &.active {
            background: @xtxColor;
          }
        }
      }
      &-btn {
        top: 110px;
        opacity: 1;
        background: rgba(0, 0, 0, 0);
        color: #ddd;
        i {
          font-size: 30px;
        }
      }
    }
  }
}
</style>
